<template>
  <div>
    <FilterTable
      ref="table"
      columnsRef="part_report_cost_price_adjust_out"
      :columns="columns"
      selectionType="single"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.selectColumns = arr)"
      :isExportExcel="true"
      :onCalcExportData="exportFun"
      exportFileName="配件综合情况查询-成本价调整出库.xlsx"
      :dataSource="dataSource"
      :isMemoryPagination="true"
    >
      <!-- :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"-->
      <template slot="controls">
        <AjaxButton size="small" type="primary" :disabled="printFlag" icon="el-icon-printer" :click="printHandle">打印</AjaxButton>
      </template>
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :click="exportHandle">导出</AjaxButton>
      </template>
    </FilterTable>
    <!-- 打印 -->
    <div v-if="showPrint">
      <BasePrint
        ref="print"
        :data="printList"
        :directPrint="printParams.vDirectPrint == 1 ? true : false"
        :direction="printParams.vPageDirection == 99921001 ? 'horizontal' : 'vertical'"
        :printerType="printParams.vPrintType == 99901001 ? 'laser' : 'stylus'"
        :alwaysPrint="printParams.vPageType == 99911002 ? true : false"
        :isDuplex="printParams.vBothPrint == 1 ? true : false"
        template="part/report/templateAdjustAndOutStorage"
      />
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartInOutStockReport, getAllUser, exportPartInOutStockReport } from '@/api/part/report/billSearch';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { dealerPrintApi } from '@/api/repair/index.js';
import { exportExcelFile } from '@/utils/index';
import moment from 'moment';
export default {
  mixins: [dictionary],
  props: {
    fetchParamObj: Object,
    type: String,
    userList: Array
  },

  data() {
    return {
      fetchApi: queryPartInOutStockReport,
      columns: this.createdTable(),
      fetchParams: { xhrAbort: true },
      selectColumns: [], // 选中行数据
      showPrint: false, // 打印显示
      printList: {}, // 打印数据
      printFlag: false, // 打印按钮是否禁用
      storageList: [],
      printParams: {},
      dataSource: []
    };
  },
  watch: {
    fetchParamObj(fetchParamObj) {
      console.log(fetchParamObj, 'fetchParamObj----');
      this.initTableData(fetchParamObj);
    }
  },
  mounted() {
    this.getStorage();
    this.getEmployee();
    // this.getPrintParams();
  },
  methods: {
    exportFun(row) {
      for (const key in row) {
        switch (key) {
          // case 'ADJUST_DATE':
          //   if (row[key]) {
          //     row[key] = moment(row[key]).format('YYYY-MM-DD');
          //   }
          //   break;
          case 'CONSULTANT':
            if (row[key]) {
              row[key] = this.userList[row[key]];
            }
            break;
          case 'STOCK_QUANTITY':
          case 'OUT_PRICE':
          case 'COST_PRICE':
          case 'OUT_AMOUNT':
          case 'COST_AMOUNT':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          default:
            break;
        }
      }
    },
    // 打印数据接口
    async getPrintData() {
      let params = Object.assign({}, this.fetchParams, { billNo: this.selectColumns[0].ADJUST_NO });

      const res = await queryPartInOutStockReport(params); // 获取当前行数据
      this.printList.detailInfo = res.data.rows;
    },
    async getPrintParams() {
      let params = { vModule: '90211009', vNormalCode: 'BJCBCK' };
      const res = await dealerPrintApi(params);
      if ((res.resultCode == 200) & res.data.length) {
        this.printParams = res.data[0];
      }

      console.log('打印的参数', this.printParams);
    },
    // 打印处理
    async printHandle() {
      // this.getPrintParams();
      // 判断是否选中数据
      if (this.selectColumns.length < 1) {
        return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
      }
      await this.getPrintData();
      this.showPrint = true;
      // 补全属性
      this.printList.baseInfo = {};
      this.printList.baseInfo.ro_no = this.selectColumns[0].ADJUST_NO;
      this.printList.baseInfo.STORAGE_CODE = this.selectColumns[0].STORAGE_CODE;
      this.printList.baseInfo.STORAGE_NAME = this.storageList[this.selectColumns[0].STORAGE_CODE];
      this.printList.baseInfo.USER_NAME = this.userList[this.printList.detailInfo[0].ADJUSTER];
      console.log(this.printList);

      try {
        this.printList.detailInfo.forEach(element => {
          element.ADJUSTER_NAME = '';
          element.ADJUSTER_NAME = this.userList[element.ADJUSTER];
        });
      } catch (error) {}
      // 打印
      this.$nextTick(() => {
        this.$refs.print.EXCUTE_PRINT();
      });
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '出库单编号', dataIndex: 'ADJUST_NO', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '原单编号', dataIndex: 'fake1', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '对应单编号', dataIndex: 'fake2', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '出库日期', dataIndex: 'ADJUST_DATE', sorter: true, filter: true, filterType: 'date-range', showOverflowTooltip: true, exportFormat: { t: 'd' } },
        // { title: '客户名称', dataIndex: 'fake3', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '牌照号', dataIndex: 'fake4', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '仓库名称', dataIndex: 'STORAGE_CODE', dictItems: [], editItems: [], sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '税率(%)', dataIndex: 'fake5', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '操作员代码', dataIndex: 'ADJUSTER', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        {
          title: '操作员名称',
          dataIndex: 'OPERATOR_NAME',
          render: props => {
            return <span>{this.userList[props.row.OPERATOR_NAME]}</span>;
          },
          dictItems: [],
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        { title: '配件代码', dataIndex: 'PART_NO', width: '160', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '配件名称', dataIndex: 'PART_NAME', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // {
        //   title: '配件说明',
        //   dataIndex: 'PART_DESCRIPTION',
        //   sorter: true,
        //   showOverflowTooltip: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // { title: 'CCC件类型', dataIndex: 'IS_CCC_PART', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '保质期', dataIndex: 'SHELF_LIFT_MARK' ,sorter:true,filter:true,filterType:'input'},
        // {
        //   title: 'ABC分类',
        //   dataIndex: 'ABC_TYPE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('8092'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.ABC_TYPE, '8092')}</span>;
        //   },
        //   showOverflowTooltip: true
        // },
        // { title: '计量单位', dataIndex: 'UNIT_NAME', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '调整数量', dataIndex: 'STOCK_QUANTITY', summation: true, sorter: true, filter: true, filterType: 'number', showOverflowTooltip: true, exportFormat: { t: 'n' } },
        { title: '出库单价', dataIndex: 'OUT_PRICE', sorter: true, filter: true, filterType: 'number', summation: false, precision: 2, showOverflowTooltip: true, exportFormat: { t: 'n' } },
        { title: '旧成本单价', dataIndex: 'COST_PRICE', sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true, exportFormat: { t: 'n' } },
        { title: '出库金额', dataIndex: 'OUT_AMOUNT', summation: true, sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true, exportFormat: { t: 'n' } },
        { title: '旧成本金额', dataIndex: 'COST_AMOUNT', summation: false, sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true, exportFormat: { t: 'n' } }
        // { title: '主组号', dataIndex: 'MAIN_GROUP_NO', sorter: true, filter: true, filterType: 'number', showOverflowTooltip: true, exportFormat: { t: 'n' } },
        // { title: '销售方式', dataIndex: 'fake7', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '发布单号', dataIndex: 'fake8', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true }
        // // {
        //   title: '成本调整单号',
        //   dataIndex: 'ADJUST_NO',
        //   sorter: true
        // },
        // {
        //   title: '仓库',
        //   dataIndex: 'STORAGE_CODE',
        //   sorter: true,
        //   editable: false,
        //   editType: 'select',
        //   editItems: []
        // },
        // {
        //   title: '库位',
        //   dataIndex: 'STORAGE_POSITION_CODE',
        //   sorter: true
        // },
        // {
        //   title: '配件代码',
        //   dataIndex: 'PART_NO',
        //   sorter: true,
        //   width: '160'
        // },
        // {
        //   title: '配件名称',
        //   dataIndex: 'PART_NAME',
        //   sorter: true
        // },
        // {
        //   title: '库存数量',
        //   dataIndex: 'STOCK_QUANTITY',
        //   sorter: true
        // },
        // {
        //   title: '成本单价',
        //   dataIndex: 'COST_PRICE',
        //   sorter: true
        // },
        // {
        //   title: '成本金额',
        //   dataIndex: 'COST_AMOUNT',
        //   sorter: true
        // },
        // {
        //   title: '新成本价',
        //   dataIndex: 'NEW_PRICE',
        //   sorter: true
        // },
        // {
        //   title: '新成本金额',
        //   dataIndex: 'NEW_AMOUNT',
        //   sorter: true
        // },
        // {
        //   title: '调整差额',
        //   dataIndex: 'diff_amount',
        //   sorter: true
        // },
        // {
        //   title: '调整日期',
        //   dataIndex: 'ADJUST_DATE',
        //   sorter: true,
        //   width: '160'
        // },
        // {
        //   title: '调整人',
        //   dataIndex: 'ADJUSTER',
        //   sorter: true,
        //   render: props => {
        //     return <span>{this.userList[props.row.ADJUSTER]}</span>;
        //   }
        // }
      ];
    },

    async initTableData(obj) {
      const fetchParamObj = this.fetchParamObj;
      // this.fetchParams = Object.assign({}, fetchParamObj, { xhrAbort: false });
      this.fetchParams = Object.assign({}, fetchParamObj);
      try {
        this.$refs.table.START_LOADING();
        this.dataSource = [];
        let res = await queryPartInOutStockReport(this.fetchParams);
        this.dataSource = res.data;
        this.$refs.table.STOP_LOADING();
      } catch (error) {
        console.log(error);
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },
    //导出
    async exportHandle(){
      let res = await exportPartInOutStockReport(this.fetchParams);
      console.log(res,"导出::");
      try{
        exportExcelFile(res.data,'成本价调整出库')
      }catch(e){
        console.log(e,"错误::");
      }
    },
    // 获取仓库
    async getStorage() {
      const res = await getStroageList();
      res.data.map(x => (this.storageList[x.STORAGE_CODE] = x.STORAGE_NAME));
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').dictItems = this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({
        text: x.STORAGE_NAME,
        value: x.STORAGE_CODE + ''
      }));
    },
    // 操作员名称
    async getEmployee() {
      const res = await getAllUser();
      // console.log(res, '操作员名称');
      if (res.resultCode == 200) {
        this.columns.find(item => item.dataIndex === 'OPERATOR_NAME').dictItems = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      }
    }
  }
};
</script>

<style lang="less" scoped>
/* .form-group {
  margin-top: 10px;
  span {
    height: 50px;
    line-height: 50px;
    text-align: right;
    width: 250px;
    padding-left: 100px;
  }
} */
</style>
